/*******************************************************************************
 * Copyright (c) 2006 Tom Schindl and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl - initial API and implementation
 *     Dinko Ivanov - bug 164365
 *******************************************************************************/

package com.gcsf.books.gui.dialogs;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * Editor Activation on DoubleClick instead of single.
 * 
 * @author Tom Schindl <tom.schindl@bestsolution.at>
 * 
 */
public class Snippet021CellEditorsOnDoubleClick {
  private class MyCellModifier implements ICellModifier {

    private TableViewer viewer;

    private boolean enabled;

    public void setEnabled(boolean enabled) {
      this.enabled = enabled;
    }

    public void setViewer(TableViewer viewer) {
      this.viewer = viewer;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object,
     * java.lang.String)
     */
    public boolean canModify(Object element, String property) {
      return enabled && ((MyModel) element).counter % 2 == 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object,
     * java.lang.String)
     */
    public Object getValue(Object element, String property) {
      return ((MyModel) element).counter + "";
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object,
     * java.lang.String, java.lang.Object)
     */
    public void modify(Object element, String property, Object value) {
      TableItem item = (TableItem) element;
      ((MyModel) item.getData()).counter = Integer.parseInt(value.toString());
      viewer.update(item.getData(), null);
    }
  }

  private class MyContentProvider implements IStructuredContentProvider {

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
     * .lang.Object)
     */
    public Object[] getElements(Object inputElement) {
      return (MyModel[]) inputElement;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
    public void dispose() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface
     * .viewers.Viewer, java.lang.Object, java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

  }

  public class MyModel {
    public int counter;

    public MyModel(int counter) {
      this.counter = counter;
    }

    public String toString() {
      return "Item " + this.counter;
    }
  }

  public Snippet021CellEditorsOnDoubleClick(Shell shell) {
    final Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
    final MyCellModifier modifier = new MyCellModifier();

    table.addListener(SWT.MouseDown, new Listener() {

      public void handleEvent(Event event) {
        modifier.setEnabled(false);
      }

    });

    final TableViewer v = new TableViewer(table);

    table.addListener(SWT.MouseDoubleClick, new Listener() {

      public void handleEvent(Event event) {
        modifier.setEnabled(true);
        TableItem[] selection = table.getSelection();

        if (selection.length != 1) {
          return;
        }

        TableItem item = table.getSelection()[0];

        for (int i = 0; i < table.getColumnCount(); i++) {
          if (item.getBounds(i).contains(event.x, event.y)) {
            v.editElement(item.getData(), i);
            modifier.setEnabled(false);
            break;
          }
        }
      }

    });

    modifier.setViewer(v);

    TableColumn column = new TableColumn(table, SWT.NONE);
    column.setWidth(200);

    v.setLabelProvider(new LabelProvider());
    v.setContentProvider(new MyContentProvider());
    v.setCellModifier(modifier);
    v.setColumnProperties(new String[] { "column1" });
    v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getTable()) });

    MyModel[] model = createModel();
    v.setInput(model);
    v.getTable().setLinesVisible(true);
  }

  private MyModel[] createModel() {
    MyModel[] elements = new MyModel[10];

    for (int i = 0; i < 10; i++) {
      elements[i] = new MyModel(i);
    }

    return elements;
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new Snippet021CellEditorsOnDoubleClick(shell);
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }

    display.dispose();

  }

}
